﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tauren.Business.DLL.Base;
using Tauren.Business.Entity.Global;
using Tauren.Business.Entity.Model;
using Global = Tauren.Business.Entity.Global;
using Model = Tauren.Business.Entity.Model;
using Dapper;
using Dapper.Contrib.Extensions;
using Tauren.Business.Infrastructure.Globals;

namespace Tauren.Business.DLL.Unitinfo
{
    /// <summary>
    /// 单位信息
    /// </summary>
    public class UnitinfoBusiness : BaseDLL, IUnitinfoBusiness
    {
        public UnitinfoBusiness(IConnectionDLLBase _dLLBase) : base(_dLLBase)
        {
        }

        /// <summary>
        /// 删除单位信息
        /// </summary>
        /// <param name="UnitId"></param>
        /// <returns></returns>
        public bool Delete(object UnitId) => dbConnection.Execute("DELETE FROM Unitinfo WHERE unitID=@UnitId", new Model.Unitinfo() { UnitId = (int)UnitId }) >= 0;
        /// <summary>
        /// 获取单个单位--根据一个编码
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public Model.Unitinfo GetByCode(string Code) => dbConnection.QueryFirstOrDefault<Model.Unitinfo>("SELECT * FROM Unitinfo WHERE unitcode=@UnitCode", new Model.Unitinfo() { UnitCode = Code });

        /// <summary>
        /// 根据多个编码获取单位信息
        /// </summary>
        /// <param name="Codes"></param>
        /// <returns></returns>
        public List<Model.Unitinfo> GetByCode(string[] Codes)
        {
            string codes = string.Join(",", Codes.Select(o => $"'{o}'"));
            return dbConnection.Query<Model.Unitinfo>($"SELECT * FROM Unitinfo WHERE unitcode IN ({codes})").ToList();
        }

        /// <summary>
        /// 分页获取单位信息
        /// </summary>
        /// <param name="queryModel"></param>
        /// <param name="Total"></param>
        /// <returns></returns>
        public List<PageUnitModel> Pages(QueryModel queryModel, out long Total)
        {
            Total = 0;
            string where = "";
            if (!string.IsNullOrEmpty(queryModel.KeyWord))
            {
                where += $" AND (UnitCode LIKE '%{queryModel.KeyWord}%' OR CNName LIKE '%{queryModel.KeyWord}%' OR ENName LIKE '%{queryModel.KeyWord}%' )";
            }
            if (string.IsNullOrEmpty(queryModel.SortField))
            {
                queryModel.SortField = "UnitId";
                queryModel.IsAsc = true;
            }
            else
            {
                queryModel.SortField = queryModel.SortField.ToLower().Replace("dic_", "").Replace("dic", "").Trim();
            }

            if (queryModel.paramters.Any())
            {
                queryModel.paramters.ForEach(p =>
                {
                    where += $"  and {p.Label} like '%{p.Value}%'  ";
                });
            }
            if (queryModel.KeyWords.Any())
            {
                queryModel.KeyWords.ForEach(p =>
                {
                    where += $"  and {p.Label}='{p.Value}'";
                });

            }
            string asc = queryModel.IsAsc ? "ASC" : "DESC";
            queryModel.Page = queryModel.Page == 0 ? 1 : queryModel.Page;
            StringBuilder sql = new StringBuilder();
            sql.Append($"SELECT  count(UnitId) as cnt FROM Unitinfo WHERE  havedelete !=1 {where};");
            sql.Append($"SELECT * FROM Unitinfo WHERE  havedelete !=1 {where}   ORDER BY {queryModel.SortField} {asc} LIMIT {(queryModel.Page - 1) * queryModel.PageSize},{ queryModel.PageSize} ;");
            List<Model.PageUnitModel> unitModels = new List<PageUnitModel>();
            using (var reader = dbConnection.QueryMultiple(sql.ToString()))
            {
                Total = reader.ReadFirstOrDefault<int>();
                unitModels = reader.Read<Model.PageUnitModel>().ToList();
                long RowNo = ((queryModel.Page == 0 ? 1 : queryModel.Page) - 1) * queryModel.PageSize + 1;
                unitModels.ForEach(o =>
                {
                    o.RowNo = RowNo;
                    RowNo++;
                });
            }
            dbConnection.Dispose();
            return unitModels;
        }
    }
}
